package com.kaibes.platform.currency.db;

import org.springframework.stereotype.Service;

import com.kaibes.orm.curd.QueryBuilder;
import com.kaibes.orm.curd.UpdateBuilder;
import com.kaibes.platform.currency.pojo.CurrencyUserUpdate;
import com.kaibes.web.api.ResponseServiceImpl;

@Service
public class CurrencyUserService extends ResponseServiceImpl<CurrencyUser> {

    public Integer getAmountByUserIdAndCurrencyId(Integer userId, Short currencyId) {
        CurrencyUser temp = get(QueryBuilder.newBuilder().eq("user_id", userId).eq("currency_id", currencyId).build());
        if (temp == null) {
            return 0;
        } else {
            return temp.getAmount();
        }
    }
    
    public CurrencyUser getByUserIdAndCurrencyId(Integer userId, Short currencyId) {
        return get(QueryBuilder.newBuilder().eq("user_id", userId).eq("currency_id", currencyId).build());
    }
    
    public int updateAmountByUserIdAndCurrencyId(CurrencyUserUpdate data) {
        return getMapper().update(UpdateBuilder.newBuilder().setValue("amount", data.getAmount())
                .addQuery(QueryBuilder.newBuilder().eq("user_id", data.getUserId()).eq("currency_id", data.getCurrencyId()).build()).build());
    }
    
}
